/**
 * Copyright 2006 Van de Wijgerd B.V. All rights reserved.
 */
package com.googlecode;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;

public class AbstractApiImpl {
    private HttpCon httpCon;
    private AuthScope authScope;
    protected UsernamePasswordCredentials credentials = null;

    public AbstractApiImpl(HttpCon httpCon) {
        this.httpCon = httpCon;
    }

    public AbstractApiImpl(HttpCon httpCon, AuthScope authScope, String nickName, String remoteKey) {
        this.httpCon = httpCon;
        this.authScope = authScope;
        this.credentials = new UsernamePasswordCredentials(nickName, remoteKey);
    }

    public AbstractApiImpl(HttpClient httpClient) {
        this.httpCon = new DefaultHttpConImpl(httpClient);
    }

    public AbstractApiImpl(HttpClient httpClient, AuthScope authScope, String nickName, String remoteKey) {
        this(httpClient);
        this.authScope = authScope;
        this.credentials = new UsernamePasswordCredentials(nickName, remoteKey);
    }

    public boolean hasCredentials() {
        return credentials != null;
    }

    protected String execute(HttpMethod method) throws ApiException {
        return httpCon.execute(method,authScope,credentials);
    }
}
